<?php
namespace Admin\Controller;
use Think\Controller;
class IndexBannerController extends IndexController{
    public function lists(){
        $IndexBanner=D("Banner");
        $type=I("request.type");
        if($type==1){
           $data['name']=I("request.name");
           $data['type']=I("request.type");
           $data['link']=I("request.link");
           $data['bz']=I('request.bz');
           $data['orders']=I('request.orders');
           $data['create_time']=time();
           if($_FILES['img']['name']!=null){
               header("content-type:text/html;charset=utf-8");
               $upload = new \Think\Upload(); // 实例化上传类
               $upload->maxSize = 1024 * 1024;// 设置附件上传大小 (-1) 是不限值大小
               $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
               $upload->rootPath = "./Public/imageBANNER/";
               $upload->replace = true; //存在同名文件是否是覆盖
               $upload->autoSub = true;  // 是否使用子目录保存上传文件
               $upload->saveName = time() . '_' . mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
               $info = $upload->uploadOne($_FILES['img']);
               if (!$info) {
                   $data['img'] = '';
               } else {
                   $data['img'] = ltrim($info['savepath'] . $info['savename'], '/');
               }
           }

            $IndexBanner->add($data);
            $this->redirect("IndexBanner/lists");
        }

        $this->display();
    }



    public function get_list(){


        $Banner=D("Banner");
        if(I("request.iDisplayStart")==''){

            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }

        $result=$Banner->limit($start,I("request.iDisplayLength"))->select();
        foreach($result as $key=>&$value){
            $value['create_time']= $value['create_time']!=''? date("Y年m月d日H时i分s秒",$value['create_time']):" ";
            $value['update_time']= $value['update_time']!=''? date("Y年m月d日H时i分s秒",$value['update_time']): "";
            //类型0为成功案例1为诊所2为月子中心3医院产品 4为活动
            switch ($value['type']) {
                case 0:
                    $value['type'] = "成功案例";
                    break;
                case 1:
                    $value['type'] = "诊所";
                    break;
                case 2:
                    $value['type'] = "为月子中心";
                    break;
                case 3:
                    $value['type'] = "医院产品";
                    break;
                case 3:
                    $value['type'] = "活动";
                    break;
            }
        }

        if($result){
            $ret = array(
                "sEcho" => intval($_GET['sEcho']),
                "iTotalRecords" => $start,
                "iTotalDisplayRecords" => $Banner->count(),
                "aaData" => $result
            );
            echo json_encode($ret);exit;

        }else{
            echo json_encode(0);
        }
    }



  		 public function add(){
        $this->display();
    }

    public function update(){
        $id=I("request.id");
        setcookie('uid',$id,time()+3600);
        $this->display();
    }

    public function update_list(){
        $IndexBanner=D("Banner");
        $map['id']=$_COOKIE['uid'];
        $result=$IndexBanner->where($map)->find();
        if($result){
            echo json_encode($result);exit;
        }else{
            echo json_encode(0);exit;
        }
    }

    public function actionUpdate(){

        $IndexBanner=D("Banner");
        $id=$_COOKIE['uid'];
        $map['id']=$id;
        $data['name']=I("request.name");
        $data['bz']=I("request.bz");
        $data['orders']=I("request.orders");
        $data['type']=I("request.type");

        if($_FILES['img']['name']!=null){
            $img=$IndexBanner->where($map)->getField("img");
            header("content-type:text/html;charset=utf-8");
            $upload = new \Think\Upload(); // 实例化上传类
            $upload->maxSize = 1024 * 1024;// 设置附件上传大小 (-1) 是不限值大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = "./Public/imageBANNER/";
            $upload->replace = true; //存在同名文件是否是覆盖
            $upload->autoSub = true;  // 是否使用子目录保存上传文件
            $upload->saveName = time() . '_' . mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
            $info = $upload->uploadOne($_FILES['img']);
            if (!$info) {
                $data['img'] = '';
            } else {
                unlink("./Public/imageBANNER/".$img);//删除上传新照片时替换掉的照片
                $data['img'] = ltrim($info['savepath'] . $info['savename'], '/');
            }
        }

            $IndexBanner->where($map)->save($data);

            $this->redirect("IndexBanner/lists");
    }

        public function del(){
            $id= I("request.id");
            if($id!=""){
                $IndexBanner=D("Banner");
                $map['id']=$id;
                $img=$IndexBanner->where($map)->getField('img');
                if($img!=""){
                    unlink("./Public/imageBANNER/".$img);
                }
                $result=$IndexBanner->where($map)->delete();
                if($result){
                    echo json_encode(1);exit;
                }
            }
        }

}

